package development.bussiness;

import state.AlertListener;
import state.GameAlert;
import state.GameForm;
import state.GameGlobal;
import state.socket.DataPackage;
import state.util.array.StringArray;
import development.Constants;
import development.FrmAcceptInviteGame;
import development.FrmDownloadResource;
import development.FrmFriend;
import development.FrmGameServiceList;
import development.Global;
import development.bean.UserBean;
import development.game.FrmAilaTrieuPhuOff;
import development.game.FrmDaoVangOffline;
import development.group.FrmGroupMenu;
import development.socket.ProtocolConstants;
import development.socket.SocketClientUtil;

public class FriendBussiness implements AlertListener {
  private String addFriendFromUser;

  public void onAddFriendNotify(DataPackage dataPackage) {
    if (Global.systemCanvas.frmCurrent == FrmGroupMenu.instance() || Global.systemCanvas.frmCurrent == GameAlert.instance() || Global.systemCanvas.frmCurrent == FrmGameServiceList.instance()
        || Global.systemCanvas.frmCurrent == FrmDownloadResource.instance() || Global.systemCanvas.frmCurrent == FrmAcceptInviteGame.instance()) {
      Global.requestFriendData.addElement(dataPackage.nextString());
    } else {
      addFriendFromUser = dataPackage.nextString();
      GameGlobal.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1( addFriendFromUser + " đề nghị được kết bạn.", "Bạn có đồng ý không?" ), 99);
    }
  }

  public void displayNextAddFriendAlert() {
    GameForm currentForm = Global.systemCanvas.frmCurrent;
    if (currentForm instanceof FrmDaoVangOffline || currentForm instanceof FrmAilaTrieuPhuOff || currentForm instanceof FrmDownloadResource || currentForm instanceof FrmAcceptInviteGame) {
      return;
    }
    
    // TODO: disable audition
//    if (currentForm instanceof FrmAuditionOff || currentForm instanceof FrmAuditionOff320x240) {
//      return;
//    }

    if (Global.requestFriendData.size() > 0) {
      addFriendFromUser = String.valueOf(Global.requestFriendData.lastElement());
      Global.requestFriendData.removeElement(Global.requestFriendData.lastElement());
      GameGlobal.alert.showAlert1(this, GameAlert.YES_NO_TYPE, StringArray.create1( addFriendFromUser + " đề nghị được kết bạn.", "Bạn có đồng ý không?" ), 99);
    }
  }

  public void onAddFriendSuccess(DataPackage dataPackage) {
    String toUser = dataPackage.nextString();
    GameGlobal.alert.showAlert(this, GameAlert.OK_TYPE, "Kết bạn với " + toUser + " thành công");
    SocketClientUtil.requestFriendList(Global.currentUser.getName());
  }

  public void onAddFriendFailUserDeny(DataPackage dataPackage) {
    String toUser = dataPackage.nextString();
    GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create2( "Kết bạn thất bại.", "Người chơi " + toUser, " từ chối kết bạn." ));
  }

  public void onAddFriendFailUserNotExist(DataPackage dataPackage) {
    String toUser = dataPackage.nextString();
    GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create2( "Kết bạn thất bại.", "Người chơi " + toUser, " không tồn tại." ));
  }

  public void onGetFriendList(DataPackage dataPackage) {
    Global.friends.removeAllElements();
    int numberOfFriend = dataPackage.nextInt();
    for (int i = 0; i < numberOfFriend; i++) {
      String username = dataPackage.nextString();
      boolean isOnline = (dataPackage.nextInt() == 1);
      String locationInfo = dataPackage.nextString();

      // Replace gameId to game name
      for (int j = 0; j < Constants.GAME_ID.len(); j++) {
        int index = locationInfo.indexOf(Constants.GAME_ID.get(j));
        if (index > 0) {
          locationInfo = locationInfo.substring(0, index) + Constants.GAME_NAME.get(j);
          break;
        }
      }

      UserBean friend = new UserBean().setUsername(username).setOnline(isOnline).setLocationInfo(locationInfo);
      Global.friends.addElement(friend);
    }
    ((FrmFriend) FrmFriend.instance()).isReceivedFriendList = true;
  }

  public void onAlreadyFriend(DataPackage dataPackage) {
    String toUser = dataPackage.nextString();
    GameGlobal.alert.showAlert(this, GameAlert.OK_TYPE, toUser + " đã là bạn.");
  }

  public void onRemoveFriendSuccess(DataPackage dataPackage) {
    String friendName = dataPackage.nextString();
    Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Hủy kết bạn với", friendName + " thành công" ));
  }

  public void onRemoveFriendFail(DataPackage dataPackage) {
    int errorCode = dataPackage.nextInt();
    String friendName = dataPackage.nextString();
    String message = "";
    switch (errorCode) {
    case ProtocolConstants.ErrorCode.NOT_FRIEND_ERROR:
      message = friendName + " không phải là bạn";
      break;
    }
    Global.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Hủy kết bạn thất bại", message ));
  }

  public void onTransferMoneySuccess(DataPackage dataPackage) {
    GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Bạn đã chuyển tiền", "thành công" ));
  }

  public void onTransferMoneyFail(DataPackage dataPackage) {
    GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Chuyển tiền không", "thành công" ));
  }

  public void onReceivedMoneySuccess(DataPackage dataPackage) {
    String fromUser = dataPackage.nextString();
    int money = dataPackage.nextInt();
    GameGlobal.alert.showAlert3(this, GameAlert.OK_TYPE, StringArray.create1( "Bạn nhận được " + money + "$", "từ người chơi " + fromUser ));
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    // Trả lời đề nghị kết bạn
    if (alertId == 99) {
      if (eventType == GameAlert.YES_BUTTON) {
        SocketClientUtil.addFriendAgree(addFriendFromUser);
      } else if (eventType == GameAlert.NO_BUTTON) {
        SocketClientUtil.addFriendDeny(addFriendFromUser);
      }
      displayNextAddFriendAlert();
    }
  }
}
